<template>
  <!-- 选择中药模板： -->
  <el-drawer title="中药模板：" size="800px" :visible="value" custom-class="drawer-box" :wrapperClosable="false" :destroy-on-close="true" @open="onOpen" @close="onClose" :append-to-body="true">
    <div v-loading="loading" class="drawer-box">
      <div>
        <div class="fl-left tree-box">
          <el-tree :data="treeData" :props="defaultProps" @node-click="onNodeClick" highlight-current>
            <span slot-scope="{ node, data }">
              <span>{{ node.label }}</span>
            </span>
          </el-tree>
        </div>
        <div class="data-right">
          <div v-for="(item,index) in templateList" :key="item.id" style="padding:10px;">
            <el-checkbox :value="checkIndex===index" @change="onChangeDrug(item,index)">{{item.name}}</el-checkbox>
          </div>
          <div v-if="templateList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
            <i class="iconfont iconwushuju" style="font-size:40px;"></i>
            <div>暂无数据</div>
          </div>
          <br>
          <pager v-model="pager" @change-pagenum="getTemplate"></pager>
          <br>
        </div>
      </div>
      <div class="">
        <div style="border-bottom:1px solid #eee;" class="p-sm">
          <div style="">已选药品：</div>
          <div class="p-sm">
            <el-tag v-for="(item,index) in curList" :key="index" :closable="false" @close="onDelTag(index)" style="margin:0 10px 10px 0;">
              {{item.string}}
            </el-tag>
          </div>
        </div>
        <div style="text-align:right;" class="p-sm">
          <el-button type="primary" @click="onSubmit" >保存</el-button>
          <el-button @click="onClose">取消</el-button>
        </div>
      </div>
    </div>
  </el-drawer>
</template>
<script>
export default {
  props: ["value", "deptId", "doctorId", "patientId"],
  components: {},
  data() {
    return {
      loading: false,
      defaultProps: {
        children: "child",
        label: "name"
      },
      checkIndex: "",
      form: {
        name: "",
        sortId: "",
        userId: "",
        deptId: ""
      }, 
      pager: { pageSize: 15, pageNum: 1, total: 0 },
      treeData: [],
      templateList: [],
      curList: []
    };
  },
  created() {
    let th = this;
  },
  mounted() {
    let th = this;
  },
  methods: {
    //删除已勾选
    onDelTag(index) {
      let th = this;
      th.curList.splice(index, 1);
    },
    //勾选药品
    onChangeDrug(obj, index) {
      let th = this;
      th.checkIndex = index;
      th.curList = obj.drugInfos || [];
    },
    onNodeClick(item, node, self) {
      this.form.sortId = item.id;
      this.checkIndex = "";
      this.getTemplate(-1);
    },
    //获取模板
    getTemplate(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {};
      Object.assign(j, th.pager, th.form);
      th.loading = true;
      th.$api.traditionalMedicineDrugTemplatepageQueryMZ(j).then(res => {
        if (res.executed) {
          th.templateList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
        th.loading = false;
      });
    },
    //获取树
    getData() {
      let th = this;
      th.loading = true;
      th.$api.getTraditionalMedicineTemplateSortTree({}).then(res => {
        if (res.executed) {
          th.treeData = res.data;
        }
        th.loading = false;
      });
    },
    onSubmit() {
      let th = this;
      //   th.getDetailCF(obj);
      let data = th.templateList[th.checkIndex];
      th.disposeData(data.id);
    },
    //处理数据
    disposeData(id) {
      let th = this;
      let j = {
        id: id
      };
      th.loading = true;
      th.$api.getTraditionalMedicineDrugTemplateId(j).then(res => {
        if (res.executed) {
          th.$emit("complete", res.data);
          th.onClose();
        }
        th.loading = false;
      });
    },
    onOpen() {
      let th = this;
      th.form.userId = th.doctorId;
      th.form.deptId = th.deptId;
      th.getData();
      th.$nextTick(function() {});
    },
    onClose() {
      let th = this;
      th.curList = [];
      th.treeData = [];
      th.templateList = [];
      th.checkIndex = "";
      th.$emit("input", false);
    }
  }
};
</script>
<style lang="scss" scoped>
.drawer-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  > div:nth-child(1) {
    flex-grow: 1;
    display: flex;
    align-items: stretch;
    .fl-left {
      flex-shrink: 0;
      width: 400px;
      overflow: auto;
      height: 100%;
      border-right: 1px solid #eee;
    }
    .data-right {
      flex-grow: 1;
      overflow: auto;
      height: 100%;
    }
  }
  > div:nth-child(2) {
    flex-shrink: 0;
    height: 300px;
    overflow: auto;
    border-top: 1px solid #eee;
  }
}
</style>

